package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.SysRoleMenuRealtion;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.SysRoleMenuRealtionMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 角色菜单关系Service
 */
@Slf4j
@Service
public class SysRoleMenuRealtionService {

    @Autowired
    private SysRoleMenuRealtionMapper sysRoleMenuRealtionMapper;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据id查询角色菜单关系
     *
     * @param sysRoleMenuRealtion
     * @return
     */
    public ResultData<SysRoleMenuRealtion> getSysRoleMenuRealtionById(SysRoleMenuRealtion sysRoleMenuRealtion) {
        log.info("根据id查询角色菜单关系:" + JSON.toJSONString(sysRoleMenuRealtion));
        if (StringUtils.isEmpty(sysRoleMenuRealtion.getId())) {
            return ResultData.createFail("id不能为空");
        }
        sysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
        SysRoleMenuRealtion data = sysRoleMenuRealtionMapper.getSysRoleMenuRealtionById(sysRoleMenuRealtion);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询角色菜单关系
     *
     * @param sysRoleMenuRealtion
     * @return
     */
    public ResultData<List<SysRoleMenuRealtion>> getSysRoleMenuRealtionList(SysRoleMenuRealtion sysRoleMenuRealtion) {
        log.info("多条件查询角色菜单关系:" + JSON.toJSONString(sysRoleMenuRealtion));
        sysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
        List<SysRoleMenuRealtion> data = sysRoleMenuRealtionMapper.getSysRoleMenuRealtionList(sysRoleMenuRealtion);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加角色菜单关系
     *
     * @param sysRoleMenuRealtion
     * @param token
     * @return
     */
    @Transactional
    public ResultData addSysRoleMenuRealtion(SysRoleMenuRealtion sysRoleMenuRealtion, String token) {
        sysRoleMenuRealtion.setId(StringUtil.getUUID());
        sysRoleMenuRealtion.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysRoleMenuRealtion.setCreateUserId(currentUser.getId());
        sysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
        log.info("添加角色菜单关系:" + JSON.toJSONString(sysRoleMenuRealtion));
        int i = sysRoleMenuRealtionMapper.addSysRoleMenuRealtion(sysRoleMenuRealtion);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("添加角色菜单关系失败");
        }
    }


    /**
     * 修改角色菜单关系
     *
     * @param sysRoleMenuRealtion
     * @return
     */
    @Transactional
    public ResultData updateSysRoleMenuRealtion(SysRoleMenuRealtion sysRoleMenuRealtion, String token) {
        if (StringUtils.isEmpty(sysRoleMenuRealtion.getId())) {
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysRoleMenuRealtion.setUpdateUserId(currentUser.getId());
        sysRoleMenuRealtion.setUpdateTime(new Date());
        log.info("修改角色菜单关系:" + JSON.toJSONString(sysRoleMenuRealtion));
        int i = sysRoleMenuRealtionMapper.updateSysRoleMenuRealtion(sysRoleMenuRealtion);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("修改角色菜单关系失败");
        }
    }


    /**
     * 删除角色菜单关系
     *
     * @param sysRoleMenuRealtion
     * @return
     */
    @Transactional
    public ResultData delSysRoleMenuRealtion(SysRoleMenuRealtion sysRoleMenuRealtion) {
        if (StringUtils.isEmpty(sysRoleMenuRealtion.getId())) {
            return ResultData.createFail("id不能为空");
        }
        sysRoleMenuRealtion.setDel(StringConstant.DEL_OK);
        log.info("删除角色菜单关系:" + JSON.toJSONString(sysRoleMenuRealtion));
        int i = sysRoleMenuRealtionMapper.deleteSysRoleMenuRealtion(sysRoleMenuRealtion);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("删除角色菜单关系失败");
        }
    }
}
